//
//  EqumentDetailVC.m
//  QoilPublic
//
//  Created by SunChao on 2018/4/13.
//  Copyright © 2018年 孙超. All rights reserved.
//

#import "EqumentDetailVC.h"
#import "EqumentEditVC.h"
#import "CoordinatesModel.h"
@interface EqumentDetailVC ()

@end

@implementation EqumentDetailVC

- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
     [QViewUtility setupNavigationBarBackForController:self backSelector:@selector(back)];
     self.title = @"设备详情";
    UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
    
    but.frame =CGRectMake(0,0, 60, 44);
    
    [but setTitle:@"编辑"forState:UIControlStateNormal];
    [but setFont:[UIFont systemFontOfSize:14]];
    [but addTarget:self action:@selector(PushToEidtVC)forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem  *barBut = [[UIBarButtonItem alloc]initWithCustomView:but];
    
    self.navigationItem.rightBarButtonItem = barBut;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    self.tableView.tableHeaderView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
}
- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [self requestData];
    
}
- (void)PushToEidtVC{
    
    UIStoryboard *mainSB = [UIStoryboard storyboardWithName:@"SCStoryboard" bundle:nil];
    EqumentEditVC *vc = [mainSB instantiateViewControllerWithIdentifier:@"EqumentEditVC"];
    vc.deviceName = self.deviceName.text;
    vc.licensePlate  = self.licensePlate.text;
    vc.operatorName = self.operatorName.text;
    vc.operatorPhone = self.operatorPhone.text;
    vc.deviceId = self.deviceId;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)requestData{
    
    NSString *url = [QParamService urlWithIOTAction:@"map/deviceDetails"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setObject:[QParamService getToken] forKey:@"token"];
    [param setObject:self.deviceId forKey:@"id"];
    
    [[QHttpManager sharedManager] POST:url parameterMD5jm:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if (error) {
            
            NSLog(@"error:%@",basModel.errMsg);
            
            return ;
        }
        
        if([basModel.result isEqualToString:@"success"]){
            CoordinatesModel *coorModel = [CoordinatesModel objectFromDict:basModel.items];
            self.oilLevel.text = [NSString stringWithFormat:@"%@%@",coorModel.oilLevel,@"%"];
            self.dailyWorkTime.text = coorModel.dailyWorkTime;
            self.workTime.text = coorModel.workTime;
            self.operatorName.text = coorModel.operatorName;
            self.operatorPhone.text = coorModel.operatorPhone;
            self.brand.text = coorModel.brand;
            self.deviceName.text = coorModel.deviceName;
            self.licensePlate.text = coorModel.licensePlate;
            self.vinNo.text = coorModel.vinNo;
            self.fuelTank.text = coorModel.fuelTank;
            self.snId.text = coorModel.snId;
            self.workTime.text = coorModel.workTime;
            self.model.text= coorModel.model;
            self.category.text = coorModel.category;
            self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
            [self.tableView reloadData];

        }
    }];
}
@end
